package com.lrs.java.jdbc.linkedpool;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

public class EasyConnectionPool implements DataSource {
	List <EasyConnection> conns =  new ArrayList<EasyConnection>();
	public EasyConnectionPool(){
		init();
	}
	private void init() {
		for (int i = 0; i < 20; i++) {
			EasyConnection conn = connectDB();
			conns.add(conn);
		}
		
	}
	private EasyConnection connectDB() {
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:ZLPD";
		try {
			conn = DriverManager.getConnection(url,"scott","tiger");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		EasyConnection easyconn = new EasyConnection(conn);
		return easyconn;
	}
	public Connection getConnection() throws SQLException {
		for (int i = 0; i < this.conns.size(); i++) {
			EasyConnection conn = conns.get(i);
			if(!conn.isBusy){
				conn.isBusy = true;
				return conn;
			}
		}
		return null;
	}

	public Connection getConnection(String username, String password)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub

	}

	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub

	}
	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

}
